﻿using System;
using System.Collections.Generic;
using System.Text;
using Kooboo.CMS.Content.Models;
using System.Web;

namespace KB
{
    public class ExcelUtility
    {
        public static StringBuilder ExcelRows(IDictionary<string, string> fieldsAndHeaders, IEnumerable<TextContent> contents)
        {
            var sb = new StringBuilder();
            sb.Append("<tr>");
            foreach (var item in fieldsAndHeaders.Keys)
            {
                var cellHeader = fieldsAndHeaders.GetString(item);
                sb.Append(" <td> <b>" + (cellHeader.IsNullOrEmpty() ? item : cellHeader) + " </b> </td>");
            }
            sb.Append(" </tr>");
            foreach (var item in contents)
            {
                sb.Append(" <tr>");

                foreach (var field in fieldsAndHeaders.Keys)
                {
                    var value = string.Empty;
                    if (field == KEY.TextContents.UtcCreationDate || field == KEY.TextContents.UtcLastModificationDate)
                    {
                        value = item.GetDateTime(field).ToLocalTime().ToString();
                    }
                    else
                    {
                        value = item.GetString(field);
                    }
                    sb.Append(" <td class='xl26'>" + value + " </td>");
                }
                sb.Append(" </tr>");
            }
            return sb;
        }
        public static string ExcelColspan(int fieldCount, string title = "")
        {
            return string.Format("<tr><td colspan=\"{0}\"><b>{1}</b></td></tr>", fieldCount, title);
        }

        private const string ExcelHeader =
                @"<html xmlns:o=""urn:schemas-microsoft-com:office:office""
                xmlns:x=""urn:schemas-microsoft-com:office:excel""
                xmlns=""http://www.w3.org/TR/REC-html40""
                 <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                 <style>
                .xl26
                 {mso-style-parent:style0;
                 font-family:""Times New Roman"", serif;
                 mso-font-charset:0;
                 mso-number-format:""@"";}
                 </style>
                 <xml>
                 <x:ExcelWorkbook>
                  <x:ExcelWorksheets>
                  <x:ExcelWorksheet>
                    <x:Name>Sheet1 </x:Name>
                    <x:WorksheetOptions>
                    <x:DefaultRowHeight>285 </x:DefaultRowHeight>
                    <x:Selected/>
                    <x:Panes>
                      <x:Pane>
                      <x:Number>3 </x:Number>
                      <x:ActiveCol>1 </x:ActiveCol>
                      </x:Pane>
                    </x:Panes>
                    <x:ProtectContents>False </x:ProtectContents>
                    <x:ProtectObjects>False </x:ProtectObjects>
                    <x:ProtectScenarios>False </x:ProtectScenarios>
                    </x:WorksheetOptions>
                  </x:ExcelWorksheet>
                  <x:WindowHeight>6750 </x:WindowHeight>
                  <x:WindowWidth>10620 </x:WindowWidth>
                  <x:WindowTopX>480 </x:WindowTopX>
                  <x:WindowTopY>75 </x:WindowTopY>
                  <x:ProtectStructure>False </x:ProtectStructure>
                  <x:ProtectWindows>False </x:ProtectWindows>
                 </x:ExcelWorkbook>
                 </xml>
                 </head> <body> <table align=""center"" style='border-collapse:collapse;table-layout:fixed'>";
        private const string ExcleFooter = "</table> </body> </html>";
        public static void Response(StringBuilder sbExcelRows, string fileName = null)
        {
            StringBuilder sbExcel = new StringBuilder(ExcelHeader);
            sbExcel.Append(sbExcelRows);
            sbExcel.Append(ExcleFooter);
            if (fileName.IsNullOrEmpty())
            {
                fileName = DateTime.Now.ToShortDateString();
            }
            ResponseFileUtility.ResponseFile(sbExcel, ResponseFileUtility.FileType.Excel, fileName);
        }
    }
}
